package org.pblue.tumblarity.data.bean;

import java.util.Date;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import org.pblue.appengine.AppengineDataException;
import org.pblue.appengine.AppengineDataManager;
import org.pblue.appengine.DataBean;

@PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
public class Event extends DataBean {

  private static final long serialVersionUID = -7650174790600555293L;
  
  @Persistent
  private Date date;
  
  @Persistent
  private String message;
  
  @Persistent 
  private String tumblelogId;
  
  public Event(Date pDate, String pMessage, Tumblelog pTumblelog) {
    super(Event.generateId(pDate, pMessage, pTumblelog.getId()), pTumblelog);
    
    this.setDate(pDate);
    this.setMessage(pMessage);
    this.setTumblelogId(pTumblelog.getId());
  }
    
  public static String generateId(Date pDate, String pMessage, String pTumblelogId) {
    return String.valueOf(pDate.getTime() + pMessage.hashCode() + pTumblelogId.hashCode());
  }
  
  public static Event get(Date pDate, String pMessage, Tumblelog pTumblelog) throws AppengineDataException {
    String id = Event.generateId(pDate, pMessage, pTumblelog.getId());
    return Event.get(id, pTumblelog);
  }

  public static Event get(String pId, Tumblelog pTumblelog) throws AppengineDataException {
    return (Event) DataBean.get(Event.class, pId, pTumblelog);
  }
  
  public static Event get(Date pDate, String pMessage, Tumblelog pTumblelog, AppengineDataManager pDataManager) throws AppengineDataException {
    String id = Event.generateId(pDate, pMessage, pTumblelog.getId());
    return Event.get(id, pTumblelog, pDataManager);
  }

  public static Event get(String pId, Tumblelog pTumblelog, AppengineDataManager pDataManager) throws AppengineDataException {
    return (Event) DataBean.get(Event.class, pId, pTumblelog, pDataManager);
  }
  
  public Date getDate() {
    return date;
  }
  
  public void setDate(Date pDate) {
    date = pDate;
  }
  
  public String getMessage() {
    return message;
  }
  
  public void setMessage(String pMessage) {
    message = pMessage;
  }
  
  public String getTumblelogId() {
    return tumblelogId;
  }
  
  public void setTumblelogId(String pTumblelogId) {
    tumblelogId = pTumblelogId;
  }

}
